package com.maila.simplerecord.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.maila.simplerecord.R;
import com.maila.simplerecord.activity.SubjectDetailActivity;
import com.maila.simplerecord.data.ProjectProgressdata;
import com.maila.simplerecord.data.Projectdata;
import com.maila.simplerecord.utils.StartActivityUtils;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/6/2.
 */

public class AllProjectAdapter extends BaseAdapter {
    Context context;
    List<Projectdata> mdata;
    int manageposition=-1;
    public AllProjectAdapter(Context context) {
        this.context = context;
        mdata = new ArrayList<>();
    }

    public void setMdata(List<Projectdata> mdata) {
        this.mdata = mdata;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mdata.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        final int pos=position;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_allproject, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
            AutoUtils.autoSize(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (pos == manageposition) {
            holder.rlManager.setVisibility(View.VISIBLE);
        } else {
            holder.rlManager.setVisibility(View.GONE);
        }

        holder.tvName.setText(mdata.get(position).project_name.substring(0, 2));
        holder.tvDetail.setText(mdata.get(position).address);
        holder.tvDate.setText(mdata.get(position).start_time + "-" + mdata.get(position).end_time);
        holder.llItemInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                manageposition=-1;
                notifyDataSetChanged();
                StartActivityUtils.startByOpt(context, SubjectDetailActivity.class, mdata.get(position).pid, 0);
            }
        });
        holder.llItemInfo.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                manageposition=position;
               notifyDataSetChanged();
                return true;
            }
        });
        holder.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMessageOK("确定删除该项目?", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ProjectProgressdata.deleteByPID(mdata.get(position).pid);
                        Projectdata.deleteByID(mdata.get(position).pid);
                        mdata.remove(position);
                        manageposition=-1;
                        notifyDataSetChanged();
                        Toast.makeText(context,"已删除",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        holder.btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMessageOK("确定发送到首页?", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    Projectdata pdata=Projectdata.getRandom(mdata.get(position).pid);
                        pdata.top=1;
                        pdata.save();
                        manageposition=-1;
                        notifyDataSetChanged();
                        Toast.makeText(context,"已发送到首页",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        return convertView;
    }


    private void showMessageOK(String message, DialogInterface.OnClickListener okListener) {
        new AlertDialog.Builder(context)
                .setMessage(message)
                .setPositiveButton("确定", okListener)
                .create()
                .show();
    }
    static class ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_detail)
        TextView tvDetail;
        @BindView(R.id.tv_date)
        TextView tvDate;
        @BindView(R.id.ll_item_info)
        RelativeLayout llItemInfo;
        @BindView(R.id.btn_delete)
        ImageView btnDelete;
        @BindView(R.id.btn_send)
        ImageView btnSend;
        @BindView(R.id.rl_manager)
        AutoRelativeLayout rlManager;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }


}
